
// 卡牌类
// 每张牌有花色、点数、牌面字符串和唯一值
// 花色：黑桃、红桃、梅花、方块
// 点数：2-10, J, Q, K, A
// 牌面字符串：如 "H2" 表示红桃2
// 唯一值：0-51，表示一副牌中的位置
// 0-12: 红桃, 13-25: 方块, 26-38: 梅花, 39-51: 黑桃
export class Card {
  suit: string; // 花色 Hearts, Diamonds, Clubs, Spades
  rank: string; // 点数 2-10, J, Q, K, A
  code: string; // 牌面字符串 Unique identifier for the card, e.g., "H2" for 2 of Hearts
  value: number; // 0-51

  constructor(id: number) {
    if (id < 0 || id > 51) {
      throw new Error("Card ID must be between 0 and 51");
    }
    this.value = id;
    const suits = ["H", "D", "C", "S"]; // 黑桃, 红桃, 梅花, 方块
    const ranks = [
      "2",
      "3",
      "4",
      "5",
      "6",
      "7",
      "8",
      "9",
      "10",
      "J",
      "Q",
      "K",
      "A",
    ];

    const suitIndex = Math.floor(id / 13); // 0~3
    const rankIndex = id % 13;             // 0~12

    this.suit = suits[suitIndex];
    this.rank = ranks[rankIndex];
    this.code = `${this.suit}${this.rank}`; // 牌面字符串, e.g., "H2" for 2 of Hearts
  }

  // 可选：返回花色 emoji
  get suitSymbol(): string {
    switch (this.suit) {
      case "S":
        return "♠";
      case "H":
        return "♥";
      case "C":
        return "♣";
      case "D":
        return "♦";
      default:
        return "";
    }
  }

  // 可选：人类可读字符串
  toString(): string {
    return `${this.suitSymbol}${this.rank}`; // 如 "♠K"
  }
}